package com.chixing.day18_thread.word;

public class ABCDemo {
    // 交替打印
    public static void main(String[] args) throws InterruptedException {
        Object a = new Object();
        Object b = new Object();
        Object c = new Object();
        Thread A = new Thread(new ThreadDemo("A",c,a));
        Thread B = new Thread(new ThreadDemo("B",a,b));
        Thread C = new Thread(new ThreadDemo("C",b,c));
        A.start();
        Thread.sleep(1);
        B.start();
        Thread.sleep(1);
        C.start();
    }
}

class ThreadDemo implements Runnable{
    public String date;
    public Object o11;
    public Object o22;
    public ThreadDemo(String date,Object o1,Object o2) {
        this.date = date;
        this.o11 = o1;
        this.o22 = o2;
    }
    @Override
    public synchronized void run() {
        int count = 0;
        while (count != 10) {
            synchronized (o11) { // c
                synchronized (o22) { // a
                    System.out.println(date); // "A"
                    count++; // 1
                    o22.notify();
                }
                try {
                    if (count == 10) {
                        o11.notify();
                    }else
                        o11.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
